<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class News extends Front_Controller {
	
	function index(){
		$this->_forward('news_category');
	}
	
	function news_category($category_alias = false){
		$page = $this->input->get_param('page',0);
		$offset = ($page > 0)?($page-1)*10:0;
		
		$this->load->model('news/news_model');
		$filter = array();
		$news = array();
		$total = 0;
		$hot_news = array();
		if ($category_alias){
			$this->load->model('categories/categories_model');
			$category = $this->categories_model->get_category_by_alias($category_alias);
			if ($category){
				$filter = array('new_category'=>$category['category_id']);
			}
			$filters = array_merge(array('status'=>1,'page'=>$offset,'limit'=>10),$filter);
			$news = $this->news_model->get_news($filters);
			$count_filters = array_merge(array('status'=>1),$filter);
			$total = $this->news_model->get_count($count_filters);
			
			$hot_news  =  $this->news_model->get_news(array('new_category'=>$category['category_id'],'status'=>1,'is_hot'=>1,'limit'=>3));
			$this->smarty->assign('hot_news',$hot_news);
		}else{
			$filter = array('category_id'=>61);
			$filters = array_merge(array('status'=>1,'page'=>$offset,'limit'=>3),$filter);
			$news = $this->news_model->get_news($filters);
			$count_filters = array_merge(array('status'=>1),$filter);
			$total = $this->news_model->get_count($count_filters);
		}
		if (is_array($news) && count($news)>0){
			$this->load->library('pagination');
			$config = array();
			$config['total_rows'] = $total;
			$config['per_page'] = '10';
			$config['num_links'] = '5';
			$config['page_query_string'] = TRUE;
			$config['query_string_segment'] = 'page';
			$config['use_page_numbers'] = TRUE;
			$this->pagination->initialize($config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign('news',$news);
			$this->smarty->assign('pagination',$pagination);
		}
		if($this->input->is_ajax()){
			$item_html = "";
			if (is_array($news) && count($news) > 0){
			foreach ($news as $new){
				$item_html .= "<li class=\"w-item\">
							<div class=\"w-row clr\"> ";
								if( $new['image'] != ''){
								$item_html .="	<div class=\"w-image\">
										<img alt=\"{$new['name']}\" src=\"{$new['image']}\">
									</div>";
								}
								$item_html .="<div ".(($new['image'] != '')?'class="w-info"':'').">
									<h2>
										<a class=\"strong\" title=\"{$new['name']}\" href=\"{$new['link']}\">{$new['name']}</a>
										<br>
										<span class=\"_cl9 _f11\">{$new['date']}</span>
									</h2>
									<div>
										<p>
										{$new['introtext']}
										</p>
									</div>
								</div>
							</div>
						</li>";
			}
			}
			echo $item_html;
		}else{
			if ($category_alias){
				$this->smarty->assign('box_title',$category['name']);
				$this->smarty->assign('page_title',$category['name']);
			}else{
				$this->smarty->assign('box_title',__('Tin tức'));
				$this->smarty->assign('page_title',__('Tin tức'));
			}
			$this->smarty->display('news_list');
		}
	}
	
	function news_detail($news_alias){
		
		$this->load->model('news/news_model');
		$new = $this->news_model->get_new_by_alias($news_alias);
		
		if ($new){
			$other_news = $this->news_model->get_news(array('other_new_id'=>$new['new_id'],'new_category'=>$new['category_id'],'limit'=>10,'status'=>1),false);
			$this->smarty->assign('other_news',$other_news);
			$this->smarty->assign('new',$new);
		}
		$this->smarty->display('news_detail');
	}
}
